import { isNull } from "@/utils/utils";
import deepCopy from "fast-copy";
/** 根据控件宽度把控件列表转成树结构 */
export const handleControlList = <T extends any[]>(list: T) => {
  const newList = deepCopy(list);
  const data: any = [];
  let children: any = [];
  let colTotal = 0;
  let id = 0;
  newList.forEach((val: any) => {
    const {
      controlAttr: { colValue },
    } = val;
    if (colValue < 24) {
      if (colTotal + colValue > 24) {
        data.push({
          controlAttr: {},
          controlId: id.toString(),
          children,
        });
        id++;
        colTotal = colValue;
        children = [val];
      } else {
        colTotal += colValue;
        children.push(val);
      }
    } else {
      if (children.length > 0) {
        data.push({
          controlAttr: {},
          controlId: id.toString(),
          children,
        });
        id++;
      }
      colTotal = 0;
      children = [];
      data.push(val);
    }
  });
  if (children.length > 0) {
    data.push({
      controlAttr: {},
      children,
      controlId: id.toString(),
    });
  }
  const result: T = data;
  return result;
};

export const handleJsonData = (str: string) => {
  let result: any = str;
  try {
    if (!isNull(str)) {
      result = JSON.parse(str);
    }
  } catch (error) {}
  return result;
};
